package cn.edu.cug.cs.gtl.docsrv.dict.controller;

import cn.edu.cug.cs.gtl.docsrv.dict.exception.BaseException;
import cn.edu.cug.cs.gtl.docsrv.dict.response.PaginatedValues;
import cn.edu.cug.cs.gtl.docsrv.dict.response.TableResponse;
import cn.edu.cug.cs.gtl.docsrv.dict.service.TableService;
import cn.edu.cug.cs.gtl.protos.TableInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;

import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

@RestController
@CrossOrigin
@RequestMapping(value = "/api")
public class TableController {

    @Autowired
    TableService tabSerice;


    @GetMapping(value = "/table/getInfo")
    public byte[] getTableInfo(
            @RequestParam(value = "tableName") String tableName) {
        try {
            TableInfo tableInfo= tabSerice.getTableInfo(tableName);
            byte[] bytes= tableInfo.toByteArray();
            return bytes;
        } catch (Exception e) {
            throw new BaseException(e.getMessage());
        }
    }

    @GetMapping(value = "/table/getStructure")
    public Object getTableStructure(
            @RequestParam(value = "tableName") String tableName) {
        try {
            return tabSerice.getTableStructure(tableName);
        } catch (Exception e) {
            throw new BaseException(e.getMessage());
        }
    }

    @GetMapping(value = "/table/getStructureV1")
    public Object getTableStructureV1(
            @RequestParam(value = "tableName") String tableName) {
        try {
            return tabSerice.getTableStructureV1(tableName);
        } catch (Exception e) {
            throw new BaseException(e.getMessage());
        }
    }


    /**
     * 获取数据中的数据记录，返回TableResponse对象
     * @param tableName 数据表名
     * @param pageNumber 要获取的页码
     * @param pageSize   每页中最多能存放的记录数
     * @return 返回TableResponse对象
     */
    @RequestMapping(value = "/table/getData")
    public TableResponse getTableData(
            @RequestParam(value = "tableName") String tableName,
            @RequestParam(value = "pageNumber", required = false) Integer pageNumber,
            @RequestParam(value = "pageSize", required = false) Integer pageSize) {
        try {
            return tabSerice.getTableData(tableName, pageNumber, pageSize);
        } catch (Exception e) {
            throw new BaseException(e.getMessage());
        }
    }


    /**
     * 获取数据中的数据记录，返回PaginatedValues对象
     * @param tableName 数据表名
     * @param pageNum 要获取的页码
     * @param pageSize   每页中最多能存放的记录数
     * @return 返回PaginatedValues对象
     */
    @RequestMapping(value = "/table/getDataV1")
    public PaginatedValues getTableDataV1(
            @RequestParam(value = "tableName") String tableName,
            @RequestParam(value = "pageNum", required = false) Integer pageNum,
            @RequestParam(value = "pageSize", required = false) Integer pageSize) {
        try {
            return tabSerice.getTableDataV1(tableName, pageNum, pageSize);
        } catch (Exception e) {
            throw new BaseException(e.getMessage());
        }
    }

    @PostMapping(value = "/table/addData")
    public void addTableData(
            @RequestParam(value = "data") String data,
            @RequestParam(value = "tableName") String tableName) {
        try {
            List<Map> columnList = JSON.parseObject(data, new TypeReference<List<Map>>(){});
            tabSerice.addTableData(tableName, columnList);
        } catch (Exception e) {
            throw new BaseException(e.getMessage());
        }
    }

    @PostMapping(value = "/table/deleteData")
    public void deleteTableData(
            @RequestParam(value = "data") String data,
            @RequestParam(value = "tableName") String tableName) {
        try {
            List<Map> columnList = JSON.parseObject(data, new TypeReference<List<Map>>(){});
            tabSerice.deleteTableData(tableName, columnList);
        } catch (Exception e) {
            throw new BaseException(e.getMessage());
        }
    }

    @PostMapping(value = "/table/updateData")
    public void updateTableData(
            @RequestParam(value = "data") String data,
            @RequestParam(value = "tableName") String tableName) {
        try {
            List<Map> columnList = JSON.parseObject(data, new TypeReference<List<Map>>(){});
            tabSerice.updateTableData(tableName, columnList);
        } catch (Exception e) {
            throw new BaseException(e.getMessage());
        }
    }

    // application/x-msdownload;
    @PostMapping(value = "/table/downloadFile")
    public void downloadTableFile(
            @RequestParam(value = "data") String data,
            @RequestParam(value = "columnName") String columnName,
            @RequestParam(value = "tableName") String tableName,
            HttpServletResponse response) throws Exception {
        response.setContentType("application/x-msdownload; charset=utf-8");
        response.setHeader("Content-Disposition", "attachment;filename=\"" + "\"");
        try {
            List<Map> columnList = JSON.parseObject(data, new TypeReference<List<Map>>(){});
            tabSerice.downloadTableBlobData(tableName, columnName, columnList, response);
        } catch (Exception e) {
            throw new BaseException(e.getMessage());
        }
    }
}
